# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np
from op_test import get_places

import paddle
from paddle import base, nn
from paddle.nn import functional


class TestNNSigmoidAPI(unittest.TestCase):
    def setUp(self):
        self.init_data()

    def init_data(self):
        self.x_shape = [10, 15]
        self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32)
        self.y = self.ref_forward(self.x)

    def ref_forward(self, x):
        return 1 / (1 + np.exp(-x))

    def ref_backward(self, y, dy):
        return dy * y * (1 - y)

    def check_static_api(self, place):
        paddle.enable_static()
        main_program = paddle.static.Program()
        mysigmoid = nn.Sigmoid(name="api_sigmoid")
        with paddle.static.program_guard(main_program):
            x = paddle.static.data(name='x', shape=self.x_shape)
            x.stop_gradient = False
            y = mysigmoid(x)
            base.backward.append_backward(paddle.mean(y))
        exe = paddle.static.Executor(place)
        out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y])
        np.testing.assert_allclose(out[0], self.y, rtol=1e-05)

        if paddle.framework.in_pir_mode():
            y_name = y.get_defining_op().name()
            self.assertTrue(y_name.startswith("pd_op.sigmoid"))
        else:
            self.assertTrue(y.name.startswith("api_sigmoid"))

    def check_dynamic_api(self, place):
        paddle.disable_static(place)
        x = paddle.to_tensor(self.x)
        mysigmoid = nn.Sigmoid()
        y = mysigmoid(x)
        np.testing.assert_allclose(y.numpy(), self.y, rtol=1e-05)

    def test_check_api(self):
        for place in get_places():
            self.check_dynamic_api(place)
            self.check_static_api(place)


class TestNNFunctionalSigmoidAPI(unittest.TestCase):
    def setUp(self):
        self.init_data()

    def init_data(self):
        self.x_shape = [10, 15]
        self.x = np.random.uniform(-1, 1, self.x_shape).astype(np.float32)
        self.y = self.ref_forward(self.x)

    def ref_forward(self, x):
        return 1 / (1 + np.exp(-x))

    def check_static_api(self, place):
        paddle.enable_static()
        main_program = paddle.static.Program()
        with paddle.static.program_guard(main_program):
            x = paddle.static.data(name='x', shape=self.x_shape)
            y = functional.sigmoid(x, name="api_sigmoid")
        exe = paddle.static.Executor(base.CPUPlace())
        out = exe.run(main_program, feed={'x': self.x}, fetch_list=[y])
        np.testing.assert_allclose(out[0], self.y, rtol=1e-05)

    def check_dynamic_api(self):
        paddle.disable_static()
        x = paddle.to_tensor(self.x)
        y = functional.sigmoid(x)
        np.testing.assert_allclose(y.numpy(), self.y, rtol=1e-05)

    def test_check_api(self):
        for place in get_places():
            self.check_static_api(place)
            self.check_dynamic_api()


class TestNNFunctionalSigmoidAPI_Compatibility(unittest.TestCase):
    def setUp(self):
        np.random.seed(123)
        paddle.enable_static()
        self.places = get_places()
        self.init_data()

    def init_data(self):
        self.shape = [10, 15]
        self.dtype = "float32"
        self.np_input = np.random.uniform(-1, 1, self.shape).astype(self.dtype)

    def ref_forward(self, x):
        return 1 / (1 + np.exp(-x))

    def test_dygraph_Compatibility(self):
        paddle.disable_static()
        x = paddle.to_tensor(self.np_input)
        paddle_dygraph_out = []
        # Position args (args)
        out1 = paddle.nn.functional.sigmoid(x)
        paddle_dygraph_out.append(out1)
        # Keywords args (kwargs) for paddle
        out2 = paddle.nn.functional.sigmoid(x=x)
        paddle_dygraph_out.append(out2)
        # Keywords args for torch
        out3 = paddle.nn.functional.sigmoid(input=x)
        paddle_dygraph_out.append(out3)
        # Tensor method args
        out4 = x.sigmoid()
        paddle_dygraph_out.append(out4)
        # Test out
        out5 = paddle.empty([])
        paddle.nn.functional.sigmoid(x, out=out5)
        paddle_dygraph_out.append(out5)
        # Reference output
        ref_out = self.ref_forward(self.np_input)
        # Check
        for i in range(len(paddle_dygraph_out)):
            np.testing.assert_allclose(
                ref_out, paddle_dygraph_out[i].numpy(), rtol=1e-05
            )
        paddle.enable_static()

    def test_static_Compatibility(self):
        main = paddle.static.Program()
        startup = paddle.static.Program()
        with base.program_guard(main, startup):
            x = paddle.static.data(name="x", shape=self.shape, dtype=self.dtype)
            # Position args (args)
            out1 = paddle.nn.functional.sigmoid(x)
            # Keywords args (kwargs) for paddle
            out2 = paddle.nn.functional.sigmoid(x=x)
            # Keywords args for torch
            out3 = paddle.nn.functional.sigmoid(input=x)
            # Tensor method args
            out4 = x.sigmoid()
            exe = base.Executor(paddle.CPUPlace())
            fetches = exe.run(
                main,
                feed={"x": self.np_input},
                fetch_list=[out1, out2, out3, out4],
            )
            ref_out = self.ref_forward(self.np_input)
            for i in range(len(fetches)):
                np.testing.assert_allclose(fetches[i], ref_out, rtol=1e-05)


if __name__ == '__main__':
    unittest.main()
